<?php              
				include "includes".DIRECTORY_SEPARATOR."start_session.php";
				
				if ( !defined('USERNAME') || !defined('GROUP') || GROUP != "admin" )
				{
					header("Location: index.php");
					exit;
				}			



  				 $tablou = array("nume" => "",
								 "mail" => "",
								 "parola" => "",
								 "grup" => "",
								 );
					
				$color = array("nume" => "#333333",
								 "mail" => "#333333",
								 "parola" => "#333333",
								 "grup" => "#333333"
								 );	
				
								 		
				 $erori = "";
				 
				 $tablou_erori = array("nume1" => "Nu a fost introdus numele<br />",
				 				 "nume2" => "Nume introdus invalid<br />",
								 "mail1" => "Nu a fost introdusa adresa de mail<br />",
								 "mail2" => "Adresa de mail invalida<br />",								 
								 "parola1" => "Nu a fost introdusa parola<br />",
								 "grup1" => "Nu a fost selectat grupul<br />",
								 );
  
  				  require_once ('./mysql_connect.php'); 
				  $rez = mysqli_query($dbc,"SELECT * FROM groups");

				   $group_options = "";				   
				   $group_options = $group_options."<option value=\"none\">...</option>";
				   
				   while($rand = mysqli_fetch_row($rez))
				   {
					  $group_options = $group_options."<option value='$rand[0]'>$rand[0]</option>";
				   }

  				  $form_get=<<<GATA
	                   <form method = POST action = "manage_users.php">
	                   <table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
	                   <tr><td><b>Nume</b></td><td><input type = "text" name = "nume" value='$tablou[nume]' size="40" maxlength="35" ></td></tr>
	                   <tr><td><b>Email</b></td><td> <input type = "text" name = "mail" value='$tablou[mail]' size="40" maxlength="35" ></td></tr>
					   <tr><td><b>Parola</b></td><td> <input type = "text" name = "parola" value='$tablou[parola]' size="40" maxlength="35" ></td></tr>
					   <tr><td><b>Grup</b></td><td> <select name="grup">'$group_options'</select></td></tr>
					   <tr><td></td><td><input type ="submit" value = "Adauga client" ></td></tr>
					   
					   </table>
	                   </form>
GATA;


			   if($_SERVER['REQUEST_METHOD'] == 'GET')
			   {
				   	echo $form_get;
			   }
  
 
  
  
  				  if($_SERVER['REQUEST_METHOD'] == 'POST')
				  
				  {
				  		$ok = 1;
								
							#verifica numele
							if(empty($_POST["nume"]))
							{
									$erori = $erori.$tablou_erori["nume1"];
									$ok = 0;
									$color["nume"] = "red";
							}
							else if((preg_match('/^[A-Za-z]+([ ,\'][A-Za-z-]+)*$/',$_POST["nume"])) == 0)
							{
									$erori = $erori.$tablou_erori["nume2"];
									$ok = 0;		
									$color["nume"] = "red";								
							}
							else
							{
									$tablou["nume"] = $_POST["nume"];  		
							}
							
						
							#verifica mail
							if(empty($_POST["mail"]))
							{
								$erori = $erori.$tablou_erori["mail1"];
								$ok = 0;							
								$color["mail"] = "red";																					
							}   
							else if((preg_match('/^([.0-9a-z_-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/',$_POST["mail"])) == 0)
							{
								$erori = $erori.$tablou_erori["mail2"];
								$ok = 0;						
								$color["mail"] = "red";																																			
							}
							else
							{
								$tablou["mail"] = $_POST["mail"];  
							}
							
					
							#verifica parola
							if(empty($_POST["parola"]))
							{
								$erori = $erori.$tablou_erori["parola1"];
								$ok = 0;		
								$color["parola"] = "red";								
							}
							else
							{
								$tablou["parola"] = $_POST["parola"];  
							}
							
							#verifica grupul
							if(empty($_POST["grup"]) || ( $_POST["grup"] == "none") )
							{
								$erori = $erori.$tablou_erori["grup1"];
								$ok = 0;		
								$color["grup"] = "red";								
							}
							else
							{
								$tablou["grup"] = $_POST["grup"];  
							}
							
							
							if($ok != 1)
							{
					
							  require_once ('./mysql_connect.php'); 
							  $rez = mysqli_query($dbc,"SELECT * FROM groups");

							  $group_options = "";				   
							  $group_options = $group_options."<option value=\"none\">...</option>";
			   
							  while($rand = mysqli_fetch_row($rez))
							  {							  
								  if( $tablou['grup'] == $rand[0] )
								  {
								  	 $group_options = $group_options."<option selected=\"selected\" value='$rand[0]'>$rand[0]</option>";
								  }
								  else
									 $group_options = $group_options."<option value='$rand[0]'>$rand[0]</option>";
							  }
				
					   			$form_post=<<<GATA
			                   <form method = POST action = "manage_users.php">
			                   <table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			                   <tr><td><b><font color='$color[nume]'>Nume</font></b></td><td><input type = "text" name = "nume" value='$tablou[nume]' size="40" maxlength="35" ></td></tr>
	    		               <tr><td><b><font color='$color[mail]'>Email</font></b></td><td> <input type = "text" name = "mail" value='$tablou[mail]' size="40" maxlength="35" ></td></tr>
							   <tr><td><b><font color='$color[parola]'>Parola</font></b></td><td> <input type = "text" name = "parola" value='$tablou[parola]' size="40" maxlength="35" ></td></tr>						   <tr><td><b><font color='$color[parola]'>Grup</font></b></td><td> <select name="grup">'$group_options'</select></td></tr>
							   <tr><td></td><td><input type = submit value = "Adauga Client" ></td></tr>
					   
							   </table>
	            		       </form>
GATA;
					 			echo $form_post;
								echo "<font color=\"red\">".$erori."</font>";
							}
							else  
							{
									require_once ('mysql_connect.php');
									$u = mysqli_query($dbc,"INSERT INTO utilizatori (nume,parola,mail,grup) VALUES ('$tablou[nume]','$tablou[parola]','$tablou[mail]','$tablou[grup]')");
									
									echo "<br />";	
									echo "<center><h2><font color=\"#00CC00\">Clientul a fost adaugat cu succes ! </font></h2></center>";	
									
									echo "<br />";	
									echo $form_get;
							}
							
					}
						
?>


